import time

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from common.Utils import QuestDriver

class QuestRegister():
    driver = ""
    url = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/register"
        self.driver = QuestDriver.driver
        self.driver.get(self.url)

    def RegisterSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys(Keys.CONTROL,'a')
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(Keys.CONTROL,'a')
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys(Keys.DELETE)

        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("B")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("0001")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()

        #确定是否注册成功
        text = self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").text
        QuestDriver.getScreenShot()
        assert text == "登 录"
        self.driver.back()

    #登录账号过长
    def RegisterFailTest1(self):
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys(Keys.CONTROL,'a')
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(Keys.CONTROL,'a')
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys(Keys.DELETE)

        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("B")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()

        #确认是否报错
        text = self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div.ant-alert.ant-alert-error > div > div").text
        QuestDriver.getScreenShot()
        assert text == "服务出了点问题"
        self.driver.refresh()


    #密码不足6位
    def RegisterFailTest2(self):
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys(Keys.CONTROL,'a')
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("B")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(Keys.CONTROL,'a')
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("0001")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("12345")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("12345")

        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()

        #确认是否报错
        text = self.driver.find_element(By.XPATH,'//*[@id="password_help"]/div').text
        QuestDriver.getScreenShot()
        assert text == "密码至少6位"
        self.driver.refresh()


    #两次输入密码不同
    def RegisterFailTest3(self):
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("B")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("0001")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("123457")

        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()

        #确认是否报错
        text = self.driver.find_element(By.CSS_SELECTOR,"#rePassword_help > div").text
        QuestDriver.getScreenShot()
        assert text == "两次密码不一致!"
        self.driver.refresh()


    def RegisterFailTest4(self):
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys(Keys.CONTROL,'a')
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(Keys.CONTROL,'a')
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys(Keys.DELETE)

        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("B")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("0001")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()

        #确认是否报错
        time.sleep(2)
        text = self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div.ant-alert.ant-alert-error > div > div").text
        QuestDriver.getScreenShot()
        assert text == "账号已存在"
        self.driver.refresh()


    #调转到登录界面
    def RegisterToLogin(self):
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(6) > a").click()

        #判断是否跳转成功
        text=self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").text
        QuestDriver.getScreenShot()
        assert text == "登 录"
        self.driver.back()




# register = QuestRegister()
# # register.RegisterSucTest()
# register.RegisterFailTest1()
# register.RegisterFailTest2()
# register.RegisterFailTest3()
# register.RegisterFailTest4()
# register.RegisterToLogin()
# QuestDriver.driver.quit()